package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.IntentUtils;
import com.slwish.common.utils.StringUtils;
import com.slwish.common.utils.TimeUtils;
import com.slwish.xty.R;
import com.slwish.xty.entity.ServiceList;
import com.slwish.xty.entity.SubscribeModel;
import com.slwish.xty.event.SubscribeEvent;
import com.slwish.xty.mvp.contract.SubscribeInfoContract;
import com.slwish.xty.mvp.presenter.SubscribeInfoPresenter;
import com.slwish.xty.utils.LocalUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 预约详情
 */
public class SubscribeInfoActivity extends PresenterToolbarActivity<SubscribeInfoContract.Presenter> implements SubscribeInfoContract.View {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_zsrs)
    TextView tvZsrs;
    @BindView(R.id.tv_jcrs)
    TextView tvJcrs;
    @BindView(R.id.tv_fwxm)
    TextView tvFwxm;
    @BindView(R.id.tv_lxr)
    TextView tvLxr;
    @BindView(R.id.tv_lxdh)
    TextView tvLxdh;
    @BindView(R.id.tv_yysj)
    TextView tvYysj;
    @BindView(R.id.tv_yyzt)
    TextView tvYyzt;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;

    private SubscribeModel model;//bean
    private boolean isshowbottom;//根据状态显示隐藏底部
    private int position;//数据对应的位置

    @Override
    protected int getLayoutId() {
        return R.layout.act_subscribe_info;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.subscribe_info_title);
        Bundle extras = getIntent().getExtras();
        model = (SubscribeModel) extras.getSerializable(Contants.MODEL);
        isshowbottom = extras.getBoolean("ISSHOWBOTTOM", false);
        position = extras.getInt("POSITION", 0);
        llBottom.setVisibility(isshowbottom ? View.VISIBLE : View.GONE);
        if (model != null) {
            tvName.setText(model.getScenicName());
            tvLocation.setText(model.getAddress());
            tvStartTime.setText(String.format(getString(R.string.rz_text), TimeUtils.getStringTime(model.getResideTime())));
            tvEndTime.setText(String.format(getString(R.string.lk_text), TimeUtils.getStringTime(model.getDepartureTime())));
            tvZsrs.setText(String.format(getString(R.string.zsrs_text), model.getDinersNum()));
            tvJcrs.setText(String.format(getString(R.string.jcrs_text), model.getStayNum()));
            tvLxr.setText(String.format(getString(R.string.lxr_content_text), model.getLinkman()));
            tvLxdh.setText(String.format(getString(R.string.lxdh_content_text), model.getPhone()));
            tvYysj.setText(String.format(getString(R.string.yysj_content_text), model.getSubscribeTime()));

            if (TextUtils.equals(model.getStatus(), "2") && TextUtils.equals(model.getSignIn(), "0")) {
                tvYyzt.setText(R.string.yy_success_title);
            } else if (TextUtils.equals(model.getStatus(), "3") && TextUtils.equals(model.getSignIn(), "0")) {
                tvYyzt.setText(R.string.yy_failed_title);
            } else if (TextUtils.equals(model.getStatus(), "4") && TextUtils.equals(model.getSignIn(), "0")) {
                tvYyzt.setText(R.string.cancel_subscribe_text);
            } else if (TextUtils.equals(model.getStatus(), "5") && TextUtils.equals(model.getSignIn(), "0")) {
                tvYyzt.setText(R.string.failed_text);
            } else if (TextUtils.equals(model.getStatus(), "8") && TextUtils.equals(model.getSignIn(), "1")
                    && TextUtils.equals(model.getIsCommented(), "0")) {
                tvYyzt.setText(R.string.completed_text);
            } else if (TextUtils.equals(model.getStatus(), "6") && TextUtils.equals(model.getSignIn(), "1")) {
                tvYyzt.setText(R.string.have_signed_in_text);
            } else if (TextUtils.equals(model.getStatus(), "8") && TextUtils.equals(model.getSignIn(), "1")
                    && TextUtils.equals(model.getIsCommented(), "1")) {
                tvYyzt.setText(R.string.have_evaluated_text);
            }
            List<ServiceList> serviceList = model.getServiceList();
            String service = "";
            for (ServiceList serviceList1 : serviceList) {
                service = service + serviceList1.getServiceName() + "  ";
            }
            tvFwxm.setText(String.format(getString(R.string.fwxm_contnet_text), service));
        }
    }

    @Override
    protected SubscribeInfoContract.Presenter initPresenter() {
        return new SubscribeInfoPresenter(this);
    }

    @OnClick({R.id.ll_sj, R.id.ll_location, R.id.ll_lxsj, R.id.btn_qd, R.id.btn_see})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_sj://查看商家
                if (model != null) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(Contants.ID, model.getMid());
                    IntentUtils.startActivity(mContext, CreditTourismInfoActivity.class, bundle);
                }
                break;
            case R.id.ll_location://查看路线
                LocalUtils.intentToMap(mContext,model.getAddress(),model.getLongitude(),model.getLatitude());
                break;
            case R.id.ll_lxsj://联系商家
                if (model ==null||StringUtils.isEmpty(model.getPhone())||model.getPhone()==null)
                    showError(R.string.toast_no_lxfs_text);
                else
                    IntentUtils.callPhone(mContext, model.getPhone());
                break;
            case R.id.btn_see://取消预约
                if (model != null)
                    mPresenter.cancelSubscribe(mContext, model.getOrderId());
                else showError(R.string.toast_yy_not_found_text);
                break;
            case R.id.btn_qd://签到
                //到签到页面
                Bundle bundle = new Bundle();
                bundle.putInt(Contants.ID, model.getOrderId());
                bundle.putInt("POSITION", position);
                IntentUtils.startActivity(mContext, SignActivity.class, bundle);
                break;
        }
    }

    /**
     * 取消预约
     *
     * @param s
     */
    @Override
    public void cancelSuccess(String s) {
        EventBus.getDefault().post(new SubscribeEvent("刷新", position));
    }
}
